package com.longtop.leaning.dao;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;

public class XMLUtil {
	public static final String TAG_FEATURE  = "feature ";
	public static final String ATTR_URL = "url";
	
	public static String getContentFromURL(String url) {
		System.out.println("http start: "+url);
		HttpConnection c = null;
		InputStream is = null;
		int rc;
		String content = null;

		try {
			c = (HttpConnection) Connector.open(url);
			rc = c.getResponseCode();
			System.out.println("code: " + rc);
			if (rc != HttpConnection.HTTP_OK) {
				throw new IOException("HTTP response code: " + rc);
			}

			is = c.openInputStream();

			// Get the ContentType
			String type = c.getType();
			System.out.println("type: " + type);

			// Get the length and process the data
			int len = (int) c.getLength();
			if (len > 0) {
				int actual = 0;
				int bytesread = 0;
				byte[] data = new byte[len];
				while ((bytesread != len) && (actual != -1)) {
					actual = is.read(data, bytesread, len - bytesread);
					bytesread += actual;
				}
				content = new String(data);
			} else {
				int ch;
				while ((ch = is.read()) != -1) {

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
				if (c != null)
					c.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println("content:"+content);
		return content;
	}

	public static Vector xml2StringVector(String xml, String tag) {
		Vector vector = new Vector();
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());

            // obtain an XML reader
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader reader = factory.createXMLStreamReader(in);

            String content = null;
            // a number representing the current XML element
            int inTag = 0;
            // there are more events to be read
            while (reader.hasNext()) {
                // get the next XML event
                switch(reader.next()) {
                    // an element starts - set the inTag value accordingly
                    case XMLStreamConstants.START_ELEMENT : 
                        String qName = reader.getLocalName();
                        if(tag.equals(qName)){
                        	inTag = 1;
                        }
                        break;
                    // an element ends - notify the user    
                    case XMLStreamConstants.END_ELEMENT : 
                        inTag = 0;
                        if (tag.equals(reader.getLocalName())) {
                        	vector.addElement(content);
                        }
                        break;
                    // read the text node content to a variable according to current tag    
                    case XMLStreamConstants.CHARACTERS : 
                    	if(inTag == 1){
                    		content = reader.getText();
                    	}
                 }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        return vector;    
    }
	
	public static void testParseSampleXML(String xml) {
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());

            // obtain an XML reader
            XMLInputFactory factory = XMLInputFactory.newInstance();
            XMLStreamReader reader = factory.createXMLStreamReader(in);

            String url = null;
            // a number representing the current XML element
            int inTag = 0;
            // there are more events to be read
            while (reader.hasNext()) {
                // get the next XML event
                switch(reader.next()) {
                    // an element starts - set the inTag value accordingly
                    case XMLStreamConstants.START_ELEMENT : 
                        String qName = reader.getLocalName();
                        System.out.println("START_ELEMENT: " + qName);
                        inTag = 0;
                        break;
                    // an element ends - notify the user    
                    case XMLStreamConstants.END_ELEMENT : 
                        inTag = 0;
                        if (TAG_FEATURE.equals(reader.getLocalName())) {
                            StringBuffer bookInfo = new StringBuffer();
                            System.out.println("END_ELEMENT: "+ TAG_FEATURE);
                        }
                        break;
                    // read the attributes 
                    // (we use attributes only in one tag so no further check is neccessary)    
                    case XMLStreamConstants.ATTRIBUTE : 
                        int count = reader.getAttributeCount();
                        for(int i=0; i<count; i++) {
                            String attrName = reader.getAttributeLocalName(i);
                            String attrValue = reader.getAttributeValue(i);
                            if (ATTR_URL.equals(attrName)) {
                            	url = attrValue;
                            	System.out.println("ATTR URL:" + url);
                            } 
                        }
                        break;
                    // read the text node content to a variable according to current tag    
                    case XMLStreamConstants.CHARACTERS : 
                        switch(inTag) {
                           
                        }
                 }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
            
    }
}
